package com.tzinfo.tzb;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.yxw.view.util.ResFinder;

public class TzbBangActiveWebActivity extends TzbBaseActivity {

	// 参数
	private String title, url;

	// UI组件
	private WebView webview;
	private TextView text_title;

	// 数据

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(ResFinder.getR(this, "R.layout.tzb_ac_main_bang_active"));
		Toast.makeText(this, "加载中…", Toast.LENGTH_SHORT).show();
		initParameter();
		init();
	}

	private void initParameter() {
		Intent thisIntent = this.getIntent();
		title = thisIntent.getStringExtra("title");
		url = thisIntent.getStringExtra("url");
	}

	private void init() {
		// 加载滚动进入条,这里没用到
		mProgressBar = (ProgressBar) ResFinder.findViewById(this,
				"R.id.progressbar_loding");
		webview = (WebView) ResFinder.findViewById(this, "R.id.webview");
		text_title = (TextView) ResFinder.findViewById(this, "R.id.text_title");
		text_title.setText(title);

		WebSettings webSettings = webview.getSettings();
		// ws.setUseWideViewPort(true);
		webSettings.setJavaScriptEnabled(true);
		// wv.addJavascriptInterface(new ContactsPlugin(), "contactsAction");

		// 设置可以支持缩放
		webSettings.setSupportZoom(true);
		// 设置默认缩放方式尺寸是far
		webSettings.setDefaultZoom(WebSettings.ZoomDensity.FAR);
		// 设置出现缩放工具

		webSettings.setBuiltInZoomControls(true);
		// 让网页自适应屏幕宽度
//		webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

		webview.loadUrl(url);

		webview.setWebChromeClient(new MyWebChromeClient());
		webview.setWebViewClient(new WebViewClient() {
			@Override
			public void onPageFinished(WebView view, String url) {
				TzbBangActiveWebActivity.this.url = url;
				super.onPageFinished(view, url);
			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				// TODO Auto-generated method stub
				super.onPageStarted(view, url, favicon);
			}

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				// Toast.makeText(FunNavWebViewActivity.this, url, 3000).show();
				webview.loadUrl(url);
				// webView.loadUrl(url);
				return true;
			}
		});
		webview.setOnKeyListener(new View.OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (event.getAction() == KeyEvent.ACTION_DOWN) {
					if (keyCode == KeyEvent.KEYCODE_BACK && webview.canGoBack()) { // 表示按返回键
						webview.goBack(); // 后退
						// webview.goForward();//前进
						return true; // 已处理
					}
				}
				return false;
			}
		});
	}

	class MyWebChromeClient extends WebChromeClient {
		@Override
		public void onProgressChanged(WebView view, int newProgress) {

			// 动态在标题栏显示进度条
			TzbBangActiveWebActivity.this.setProgress(newProgress * 100);
			super.onProgressChanged(view, newProgress);
		}

		@Override
		public void onReceivedTitle(WebView view, String title) {
			// 设置当前activity的标题栏
			TzbBangActiveWebActivity.this.setTitle(title);
			super.onReceivedTitle(view, title);
		}
	}

}
